<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 员工控制器
 */
class Staff extends AdminBase
{
    /**
     * 在职员工列表
     */
    public function staffList()
    {
        error_reporting(E_ERROR | E_PARSE );   
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        // if($this->param['company_id']){
            $this->assign('branch_list', $this->logicBranch->getBranchList([],'a.*,b.name','',false));
        // }
        // if($this->param['branch_id'] && $this->param['company_id']){
            $this->assign('group_list', $this->logicgroup->getGroupList([],'a.*,b.name,c.branch_name','',false));
        // }
        $w_status=empty($this->param['w_status'])?2:$this->param['w_status'];
        $where = $this->logicStaff->getWhere($this->param);
        $this->assign('w_status',$w_status);
        $this->assign('list', $this->logicStaff->getStaffList($where));
        return $this->fetch('staff_list');
    }


    public function aiImport(){
        // return 1111;
        return $this->logicStaff->aiImport();
    }
    
      /**
     * 数据状态设置
     */
    public function setStatus()
    {  
      $this->jump($this->logicAdminBase->setStatus('Staff', $this->param));
    }

    /**
     * 待入职员工列表
     */
    public function staffRelation()
    {
        error_reporting(E_ERROR | E_PARSE ); 
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        if($this->param['company_id']){
            $this->assign('branch_list', $this->logicBranch->getBranchList(['company_id'=>$this->param['company_id']],'a.*,b.name','',false));
        }
        if($this->param['branch_id'] && $this->param['group_id']){
            $this->assign('group_list', $this->logicgroup->getgroupList(['branch_id'=>$this->param['branch_id']],'a.*,b.name,c.branch_name','',false));
        }
        $where = $this->logicStaff->getWhere($this->param);
        $where['a.w_status']=1;
        $this->assign('list', $this->logicStaff->getStaffList($where));
        return $this->fetch('staff_relation');
    }

    /**
    * 导出选择
    */
    public function staffExport() 
    { 
        $this->view->engine->layout(false);
        return $this->fetch('staff_export');  
    }

   /**
    * 员工导入
    */

    public function staffDataImport() 
    { 
        error_reporting(E_ERROR | E_PARSE ); 
        $test_url=$this->param['test_url'];
        $type=$this->param['type'];
        return json($this->logicStaff->staffDataImport($test_url,$type));
    }

    public function staffDataImportS(){
        // return 11;
        $this->logicStaff->staffDataImportS();
    }
    /**
    * 导入文件
    */
    public function staffImport() 
    { 
        $this->view->engine->layout(false);
        $this->assign('type',$this->param['type']);
        switch ($this->param['type']) {
            case 0:
                $msg='待入职员工导入';
                break;
            case 1:
                $msg='员工基本信息导入';
                break;
            case 2:
                $msg='员工培训经历导入';
                break;
            case 3:
                $msg='员工工作经历导入';
                break;
            case 4:
                $msg='员工奖惩记录导入';
                break;
            case 5:
                $msg='员工月薪导入';
                break;
            default:
                break;
        }
        $this->assign('msg',$msg);
        return $this->fetch('staff_import');  
    }

    /**
     * 入职员工添加
     */
    public function staffAdd()
    {
        IS_POST && $this->jump($this->logicStaff->staffAdd($this->param));
        // IS_POST && $this->logicStaff->staffAdd($this->param);
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        $this->assign('branch_list', $this->logicBranch->getBranchList($where = [],'a.*,b.name','',false));
        $this->assign('group_list', $this->logicGroup->getGroupList($where = [],'a.*,b.name,c.branch_name','',false));
        return $this->fetch('staff_add');
    }
    
    /**
     * 待入职员工添加
     */
    public function staffRelationAdd()
    {
        IS_POST && $this->jump($this->logicStaff->staffRelationAdd($this->param));
        // IS_POST && $this->logicStaff->staffAdd($this->param);
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        $this->assign('branch_list', $this->logicBranch->getBranchList($where = [],'a.*,b.name','',false));
        $this->assign('group_list', $this->logicGroup->getGroupList($where = [],'a.*,b.name,c.branch_name','',false));
        return $this->fetch('staff_relation_add');
    }

    /**
     * 入职员工编辑
     */
    public function staffEdit()
    {
        if(IS_POST){
            if($this->param['id'] == '0' || empty($this->param['id'])){
                return [RESULT_ERROR, '数据异常，请联系管理员'];
            }else{
                $this->jump($this->logicStaff->staffEdit($this->param));
            }
            
        }
        $info = $this->logicStaff->getStaffInfo(['a.id' => $this->param['id']]);
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        $this->assign('branch_list', $this->logicBranch->getBranchList(['company_id'=>$info['company_id']],'a.*,b.name','',false));
        $this->assign('group_list', $this->logicGroup->getGroupList(['branch_id'=>$info['branch_id']],'a.*,b.name,c.branch_name','',false));
        // dump($this->logicStaff->staffSchool(['s_staff_id' => $this->param['id']]));exit();
        $this->assign('staff_school', $this->logicStaff->staffSchool(['s_staff_id' => $this->param['id']]));
        $this->assign('staff_work', $this->logicStaff->staffWork(['w_staff_id' => $this->param['id']]));
        $this->assign('staff_train', $this->logicStaff->staffTrain(['t_staff_id' => $this->param['id']]));
        $this->assign('staff_child', $this->logicStaff->staffChild(['ch_staff_id' => $this->param['id']]));
        $this->assign('staff_clan', $this->logicStaff->staffClan(['c_staff_id' => $this->param['id']]));
       
        $this->assign('info', $info);
        
        return $this->fetch('staff_edit');
    }
    
    /**
     * 待入职员工编辑
     */
    public function staffRelationEdit()
    {
        if(IS_POST){
            if($this->param['id'] == '0' || empty($this->param['id'])){
                return [RESULT_ERROR, '数据异常，请联系管理员'];
            }else{
                $this->jump($this->logicStaff->staffRelationEdit($this->param));
            }
            
        }
        $info = $this->logicStaff->getStaffInfo(['id' => $this->param['id']]);
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        $this->assign('branch_list', $this->logicBranch->getBranchList(['company_id'=>$info['company_id']],'a.*,b.name','',false));
        $this->assign('group_list', $this->logicGroup->getGroupList(['branch_id'=>$info['branch_id']],'a.*,b.name,c.branch_name','',false));
        
        $this->assign('info', $info);
        
        return $this->fetch('staff_relation_edit');
    }

    /**
     * 教育经历删除
     */

    public function staffSchoolDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffSchoolDel(['id' => $id]));
    }
/**
     * 培训经历删除
     */

    public function staffTrainDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffTrainDel(['id' => $id]));
    }
/**
     * 工作经历删除
     */

    public function staffWorkDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffWorkDel(['id' => $id]));
    }
/**
     * 直系亲属删除
     */

    public function staffClanDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffClanDel(['id' => $id]));
    }
/**
     * 自定义备注删除
     */

    public function staffChildDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffChildDel(['id' => $id]));
    }

    /**
     * 员工删除
     */

    public function staffDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffDel(['id' => $id]));
    }

     /**
     * 待入职员工删除
     */

    public function staffRelationDel($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffRelationDel(['id' => $id]));
    }

    /**
     * 员工入职

     */

    public function staffWorkAudit($id = 0)
    {
        
       $this->jump($this->logicStaff->staffWorkAudit($this->param));
    }

    /**
     * 员工离职

     */

    public function staffWorkAudits($id = 0)
    {
        
        IS_POST &&  $this->jump($this->logicStaff->staffWorkAudits($this->param));

        $this->view->engine->layout(false);
        $this->assign('id',$this->param['id']);
        return $this->fetch('staff_workaudit');
    }

    /**
     * 员工审核

     */

    public function staffAudit($id = 0)
    {
        
        return $this->jump($this->logicStaff->staffAudit(['id' => $id,'audit_status'=>$this->param['audit_status']]));
    }

    /**
     * 员工调职

     */

    public function staffEditPosition($id = 0)
    {
        $info = $this->logicStaff->getStaffList(['a.id' => $this->param['id']]);
        $this->assign('info', $info[0]);
        $list_p=$this->logicStaff->getPositionList(['a.staff_id' => $this->param['id']]);
        if(count($list_p)==0){
            $data=array(
                'company_id'=>$info[0]['company_id'],
                'branch_id'=>$info[0]['branch_id'],
                'group_id'=>$info[0]['group_id'],
                'staff_id'=>$info[0]['id'],
                
                'p_name'=>$info[0]['position'],
                'move_time'=>$info[0]['entry_time']
            );
            $this->logicStaff->staffPositionAdd($data);
        }
        $list=$this->logicStaff->getPositionList(['a.staff_id' => $this->param['id']]);
        $this->assign('list',$list);
        return $this->fetch('staff_editposition');
    }

    /**
     * 员工调职添加

     */

    public function staffPositionAdd($id = 0)
    {
        IS_POST && $this->jump($this->logicStaff->staffPositionAdd($this->param));
        $this->assign('company_list', $this->logicCompany->getCompanyList($where = [],true,'create_time desc',false));
        $this->assign('branch_list', $this->logicBranch->getBranchList($where = [],'a.*,b.name','',false));
        $this->assign('group_list', $this->logicGroup->getGroupList($where = [],'a.*,b.name,c.branch_name','',false));
        $this->assign('staff_id',$this->param['staff_id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_position_add');
    }

    /**
     * 员工薪资
     
     */

    public function staffPrice($id = 0)
    {
        IS_POST && $this->jump($this->logicStaff->staffPriceEdit($this->param));
       $info = $this->logicStaff->staffPrice(['id' => $this->param['id']]);
        $month_price = $this->logicStaff->staffMonthPrice(['staff_id' => $this->param['id']]);
        $this->assign('info', $info);
        $this->assign('month_price', $month_price);
        return $this->fetch('staff_price');
    }

    /**
     * 员工调薪编辑
     */
    public function staffAddPriceEdit()
    {
       if(IS_POST){
            if($this->param['id'] == '0' || empty($this->param['id'])){
                return [RESULT_ERROR, '数据异常，请联系管理员'];
            }else{
                $this->jump($this->logicStaff->staffAddPriceEdit($this->param));
            }
            
        }
        $info = $this->logicStaff->staffAddPrice(['id' => $this->param['id']]);
        $this->assign('info', $info);
        $this->assign('staff_id',$this->param['staff_id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_addprice_edit');
    }

    /**
    * 员工调薪列表
    */
    public function staffAddPriceList() 
    { 
        $this->assign('list', $this->logicStaff->staffAddPriceList(['b.staff_id' => $this->param['id']]));
        $this->assign('staff_id',$this->param['id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_addprice_list');  
    }

    /**
     * 员工调薪添加
     */
    public function staffAddPriceAdd()
    {
        IS_POST && $this->jump($this->logicStaff->staffAddPriceAdd($this->param));
        $this->assign('staff_id',$this->param['staff_id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_addprice_add');
    }


    /**
     * 员工调薪删除
     */
    public function staffAddPriceDel($id = 0)
    {
        return $this->jump($this->logicStaff->staffAddPriceDel(['id' => $id]));
    }


    /**
     * 员工月薪编辑
     */
    public function staffMonthPriceEdit()
    {
       if(IS_POST){
            if($this->param['id'] == '0' || empty($this->param['id'])){
                return [RESULT_ERROR, '数据异常，请联系管理员'];
            }else{
                $this->jump($this->logicStaff->staffMonthPriceEdit($this->param));
            }
            
        }
        $info = $this->logicStaff->staffMonthPrice(['id' => $this->param['id']]);
    
        $this->assign('info', $info);
        $this->assign('staff_id',$this->param['staff_id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_monthprice_edit');
    }

    /**
    * 员工月薪列表
    */
    public function staffMonthPriceList() 
    { 
        $this->assign('list', $this->logicStaff->staffMonthPriceList(['b.staff_id' => $this->param['id']]));
        $this->assign('staff_id',$this->param['id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_monthprice_list');  
    }

    /**
     * 员工月薪添加
     */
    public function staffMonthPriceAdd()
    {
        IS_POST && $this->jump($this->logicStaff->staffMonthPriceAdd($this->param));
        $this->assign('staff_id',$this->param['staff_id']);
        $this->view->engine->layout(false);
        return $this->fetch('staff_monthprice_add');
    }


    /**
     * 员工月薪删除
     */
    public function staffMonthPriceDel($id = 0)
    {
        return $this->jump($this->logicStaff->staffMonthPriceDel(['id' => $id]));
    }

    /**
     * 待入职员工导出
     */
    public function exportStaffRelation()
    {
        $where = $this->logicStaff->getWhere($this->param);
        $where['a.w_status']=1;
        $this->logicStaff->exportStaffRelation($where);
    }

    /**
     * 员工基本资料导出
     */
    public function exportStaffBasicList()
    {
        $where = $this->logicStaff->getWhere($this->param);
        $this->logicStaff->exportStaffBasicList($where);
    }

    /**
   * 导出员工其它信息
   */
    public function exportStaffOther()
    {
        $where = $this->logicStaff->getWhere($this->param);
        $this->logicStaff->exportStaffOther($where,$this->param['type']);   
    }

    //员工个人信息word下载
    public function staffWord(){
        $html =$this->staffDetails($this->param['xiazai'],$this->param['id']) ;
        $this->start(); 
        $name=urldecode($this->param['name']);
        $wordname =$name."个人详细信息.docx"; 
        echo $html; 
        $this->save($wordname);
        header('Content-type: application/word');
        header("Content-Disposition: attachment; filename=$wordname");
        readfile($wordname); 
        ob_flush();//每次执行前刷新缓存 
        flush(); 
        exit();
    }

    //详情页
    public function staffDetails($xiazai='',$id=''){
        if(empty($id)){
            $id=$this->param['id'];
        }
        $staff = $this->logicStaff->getStaffInfo(['a.id' => $id]);
        $picture=$this->logicStaff->staffPicture(['id' => $staff['img_id']]);
        if($picture){
            $staff['href']="http://erp.dscl518.com/upload/picture/".$picture['path'];
        }else{
            $staff['href']='http://erp.dscl518.com/static/module/admin/img/onimg.png'; 
        }
        $this->assign('staff_work',$this->logicStaff->staffWork(['w_staff_id' => $id]));//工作经历
        $this->assign('staff_train', $this->logicStaff->staffTrain(['t_staff_id' => $id]));//培训经历
        $this->assign('staff_child',$this->logicStaff->staffChild(['ch_staff_id' => $id]));//自定义备注
        $staff['sex']=$staff['sex']==1?'男':'女';
        $staff['marry_status']=$staff['marry_status']==1?'已婚':'未婚';
        if($staff['census_type']==1){
          $staff['census_type']='农村户口';
        }else if($staff['census_type']==2){
          $staff['census_type']='城镇户口';
        }else if($staff['census_type']==3){
          $staff['census_type']='外埠农村';
        }else{
          $staff['census_type']='外埠城镇';
        }
        $this->assign('staff',$staff);
        $this->assign('xiazai',$xiazai);
        if(!empty($xiazai)){
        $this->view->engine->layout(false);
        }
        return $this->fetch('staff_details');
    }

    public function getPassword(){
        echo data_md5_key($this->param['pa']);
    }


}
